package com.xxy.controller;

import com.xxy.common.Result;
import com.xxy.pojo.Article;
import com.xxy.services.ArticleService;
import com.xxy.utils.JwtUtil;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author xingyu Xue
 * @Date 2024/1/29 18:39
 *
 * article 文章管理相关接口
 */
@RestController
@Slf4j
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    ArticleService articleService;

    @GetMapping("/list")
    public Result<String> list(@RequestHeader("Authorization") String token, HttpServletResponse httpServletResponse){
        //验证token
        try {
            //能解析出token说明就是登录过来，因为这个token就是登录时生成的。
            Map<String, Object> claims = JwtUtil.parseToken(token);
            return Result.success("获取所有文章数据~~~");
        } catch (Exception e) {
            e.printStackTrace();
            httpServletResponse.setStatus(401);
            return Result.error("未登录");
        }


    }

    /**
     * 接口描述：该接口用于新增文章(发布文章)
     * @param article
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Article article){
        log.info("contet:{}",article.getContent());
        log.info("title:{}",article.getTitle());
        log.info("cover_img:{}",article.getCover_img());
        log.info("state:{}",article.getState());
        log.info("categoryId:{}",article.getCategory_id());


        articleService.add(article);
        return Result.success("发布文章~~~");
    }

    /**
     * 接口描述：该接口用于根据ID获取文章详细信息
     * @param id
     * @return
     */
    @GetMapping
    public Result getArticleById(@RequestParam("id") Integer id){
        Article article = articleService.getArticleById(id);
        return Result.success(article);
    }


}
